/*
 * FormFields.java
 *
 * Created on May 13, 2006, 6:00 PM
 */

/**
 *
 * @author  Timoz
 */
import com.mysql.jdbc.Driver;
import java.sql.*;
import java.net.*;
import javax.swing.*;
import java.awt.event.*;

/** A JPanel that handles the selection of the table and the Fields that the
 *  Form Wizard will create the Form from.
 */

public class FormFields extends javax.swing.JPanel {
    java.util.Vector availableFields;  //Vector to store the Fields available in the user's selected Table
    java.util.Vector availableTables;  //Vector to store the Tables available in the user's selected Database'
    FormWizard frame;
    
    /** Creates new form FormFields */
    public FormFields(FormWizard frame) {
        this.frame=frame;
        initComponents();
        availableFields = new java.util.Vector();
        jButton6.setEnabled(false);  //set the Back button by false (as it is the first step in the Wizard)
        MainWindow.dbCon.setDatabase(MainWindow.selectedDatabase);
        availableTables=MainWindow.dbCon.getTables();  //get the tables in the user's selected database
        //if the user hasn't selected any table yet (hasn't pressed back in one of the next steps)
        //then get the fields of the first table in the database
            if (frame.selectedTable==null) {
                frame.selectedTable=(String)availableTables.get(0);
                availableFields=MainWindow.dbCon.getFields((String)availableTables.get(0));
            }
            
        //set the Combo Box Model to the available tables 
            jComboBox1.setModel(new DefaultComboBoxModel(availableTables));
            
            //if the user has already selected a table (he pressed back in one of the next steps)
            //then set the selected table in the ComboBox
            //and get the Fields of that selected Table
            if (frame.selectedTable!=null) {
                if (frame.selectedFields.isEmpty()) {
                    jComboBox1.setSelectedItem(frame.selectedTable);
                    availableFields=MainWindow.dbCon.getFields(frame.selectedTable);
                } else {
                    jComboBox1.setSelectedItem(frame.selectedTable);
                }
            }
            jList1.setListData(availableFields);
            jList2.setListData(frame.selectedFields);
            jComboBox1.addItemListener(new TableSelectionHandler());
    }
    
    /** Item Listener For the Combo Box that stores the  available Tables
     */
    
    class TableSelectionHandler implements ItemListener {
        public void itemStateChanged(ItemEvent a) {
            frame.selectedTable=(String)jComboBox1.getSelectedItem();
            availableFields = MainWindow.dbCon.getFields((String)(jComboBox1.getSelectedItem()));
            frame.selectedFields.removeAllElements();
            jList2.setListData(frame.selectedFields);
            jList1.setListData(availableFields);
        }
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jButton4 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jList2 = new javax.swing.JList();
        jSeparator1 = new javax.swing.JSeparator();
        jButton5 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jButton7 = new javax.swing.JButton();
        jButton8 = new javax.swing.JButton();
        jComboBox1 = new javax.swing.JComboBox();
        jSeparator2 = new javax.swing.JSeparator();

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel1.setText("Tables");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel2.setText("Available Fields:");

        jScrollPane1.setViewportView(jList1);

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel4.setText("What Fields do you want on your Form?");

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel5.setText("You can choose from only one Table");

        jButton4.setText("<<");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jButton2.setText(">>");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton1.setText(">");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });

        jButton3.setText("<");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel3.setText("Selected Fields:");

        jScrollPane2.setViewportView(jList2);

        jButton5.setText("Cancel");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jButton6.setText("< Back");

        jButton7.setText("Next >");
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });

        jButton8.setText("Finish");
        jButton8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton8ActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(jSeparator1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(21, 21, 21)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel5)
                            .add(jLabel4))
                        .addContainerGap(181, Short.MAX_VALUE))
                    .add(layout.createSequentialGroup()
                        .add(jComboBox1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 186, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .add(layout.createSequentialGroup()
                        .add(jLabel1)
                        .addContainerGap())
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, jSeparator2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 409, Short.MAX_VALUE)
                            .add(layout.createSequentialGroup()
                                .add(jButton5)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 133, Short.MAX_VALUE)
                                .add(jButton6)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(jButton7)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(jButton8, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 67, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                            .add(layout.createSequentialGroup()
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(layout.createSequentialGroup()
                                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 149, Short.MAX_VALUE)
                                        .add(16, 16, 16)
                                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                                                .add(jButton1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .add(jButton2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .add(jButton3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                            .add(jButton4))
                                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 22, Short.MAX_VALUE))
                                    .add(layout.createSequentialGroup()
                                        .add(jLabel2)
                                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)))
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 165, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                    .add(jLabel3))
                                .add(8, 8, 8)))
                        .add(20, 20, 20))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jSeparator1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 10, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                                .add(23, 23, 23)
                                .add(jLabel4)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(jLabel5)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 26, Short.MAX_VALUE)
                                .add(jLabel1)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(jComboBox1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .add(14, 14, 14)
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                                    .add(jLabel2)
                                    .add(jLabel3))
                                .add(116, 116, 116)))
                        .add(23, 23, 23))
                    .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                        .add(176, 176, 176)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 120, Short.MAX_VALUE)
                            .add(layout.createSequentialGroup()
                                .add(jButton1)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(jButton2)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 16, Short.MAX_VALUE)
                                .add(jButton3)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(jButton4))
                            .add(jScrollPane1, 0, 0, Short.MAX_VALUE))))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jSeparator2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 10, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jButton8)
                    .add(jButton7)
                    .add(jButton6)
                    .add(jButton5))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked
// TODO add your handling code here:
    }//GEN-LAST:event_jButton1MouseClicked
    
    /**
     *Action Performed Handler for the Finish Button
     */
    private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton8ActionPerformed
        if (frame.selectedFields.isEmpty()) {
            //if the user hasn't selected any Fields from a Table
            //Issue an error message
            JOptionPane.showMessageDialog(frame,"You Must select Fields for your Form","Error",JOptionPane.ERROR_MESSAGE);
        } else {
            frame.createForm();
        }
    }//GEN-LAST:event_jButton8ActionPerformed
    
     /**
     *Action Performed Handler for the Next Button
     */
    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
        if (frame.selectedFields.isEmpty()) {
            JOptionPane.showMessageDialog(frame,"You Must select Fields for your Form","Error",JOptionPane.ERROR_MESSAGE);
        } else {
            frame.remove(this);
            frame.add(new FormLayouts(frame));
            frame.validate();
            frame.repaint();
        }
    }//GEN-LAST:event_jButton7ActionPerformed
    
     /**
     *Action Performed Handler for the Cancel Button
     */
    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        frame.dispose();
    }//GEN-LAST:event_jButton5ActionPerformed
    
     /**
     *Action Performed Handler for the < Button
     */
    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        Object selectedValues[]=jList2.getSelectedValues();
        String tableName=(String)jComboBox1.getSelectedItem();
        if (jList2.getSelectedIndex()!= -1) {
            for (Object str : selectedValues)
            {
            if (tableName.equals(((String)str).substring(0,((String)str).indexOf('.')))) {
                availableFields.add(((String)str).substring(((String)str).indexOf('.')+1));
            }
            frame.selectedFields.remove(str);
        }
        }
        jList1.setListData(availableFields);
        jList2.setListData(frame.selectedFields);
    }//GEN-LAST:event_jButton3ActionPerformed
    
     /**
     *Action Performed Handler for the > Button
     */
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        
        if (jList1.getSelectedIndex()!= -1) {
            Object selectedValues[] = jList1.getSelectedValues();
            for (Object str : selectedValues)
            {
            frame.selectedFields.add(jComboBox1.getSelectedItem()+"."+(String)str);
            availableFields.remove(str);
            }
        }
        jList1.setListData(availableFields);
        jList2.setListData(frame.selectedFields);
    }//GEN-LAST:event_jButton1ActionPerformed
    
     /**
     *Action Performed Handler for the >> Button
     */
    
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        if (availableFields.isEmpty()==false) {
            int i=0;
            while (i<availableFields.size()) {
                frame.selectedFields.add(jComboBox1.getSelectedItem()+"."+availableFields.get(i));
                i++;
            }
            availableFields.removeAllElements();
            jList1.setListData(availableFields);
            jList2.setListData(frame.selectedFields);
        }
    }//GEN-LAST:event_jButton2ActionPerformed
    
     /**
     *Action Performed Handler for the << Button
     */
    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        if (frame.selectedFields.isEmpty()==false) { //check if the selected fields are empty
            int i=0;
            while (i<frame.selectedFields.size()) {
                String str=(String)frame.selectedFields.get(i);
                String tableName=(String)jComboBox1.getSelectedItem();
                if (tableName.equals(str.substring(0,str.indexOf('.')))) {
                    availableFields.add(str.substring(str.indexOf('.')+1));
                }
                i++;
            }
            frame.selectedFields.removeAllElements();
            jList1.setListData(availableFields);
            jList2.setListData(frame.selectedFields);
        }
    }//GEN-LAST:event_jButton4ActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton8;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JList jList1;
    private javax.swing.JList jList2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    // End of variables declaration//GEN-END:variables
    
}
